module V1
    class Qx < Grape::API
        get 'hotel_info' do
            sections_id = params[:sections_id]    #为了组织机构，新增参数
            ref=HotelMaster.where( "sections_id=?", sections_id)
            {ref:0,msg:"提交成功",result:ref }
        end

        get 'hotel_address' do
            hotel_id = params[:hotel_id]
            ref=HotelInfo.find_by_sql(" SELECT id ,name,address FROM  hotel_info
                         where master_id=#{hotel_id}  ")
            {ref:0,msg:"提交成功",result:ref }
        end
        
        get 'section_info' do
            ref=Section.find_by_sql("SELECT id,section_name name FROM  sections
WHERE id<>0")
            {ref:0,msg:"提交成功",result:ref }
        end

        post 'hotel_record' do
            sections_id = params[:sections_id]    #为了组织机构，新增参数
            address_id =params[:address_id]   #地址id
            openid =params[:openid]           #openid
            name=params[:name]                #姓名
            phone=params[:phone]              #手机号
            idcard=params[:idcard]            #身份证
            pic=params[:pic]               #图片
            sex=params[:sex]          #性别
            bir=params[:bir]          #生日
            time=Time.now.strftime("%Y-%m-%d %H:%M:%S")         #入驻时间
            status='未审核'       #状态 
            data=RecordData.new              
            data.hotel_address_id=address_id
            data.open_id=openid
            data.name=name
            data.phone=phone
            data.birthday=bir
            data.sex=sex
            data.idcard=idcard
            data.created_at=Time.now.strftime("%Y-%m-%d %H:%M:%S")
            data.status=status
            data.upload_time=time
            data.sections_id=sections_id  #需要保持组织机构id
            data.save
            puts params 
            if ! pic.blank?                  
                filename = uploadfile(pic,data.id)
                if filename=="toobig"
                   notice= "照片的大小超过了6M"
                   data.destroy
                   return  {ref:1,msg:"失败",result:notice }      
                end
            end
            data.pic = filename
            data.save
            {ref:0,msg:"提交成功",result:"" }
        end
      ################################################################
       get 'alarm_upload' do
            openida =params[:openid]           #openid
            name=params[:name]                #姓名
            phone=params[:phone]              #手机号
            address=params[:address]            #身份证
            msg=params[:msg]  
            sections_id = params[:sections_id]    #为了组织机构，新增参数             
            status='未审核'       #状态
            # ref=HotelInfo.find(address_id)
            data=AlarmRecord.new
            data.address=address
            data.openid=openida
            data.name=name
            data.phone=phone
            data.status=status
            data.message=msg
            data.sections_id=sections_id
            data.created_at=Time.now.strftime("%Y-%m-%d %H:%M:%S")
            if data.save
              {ref:0,msg:"提交成功",result:"" }
            else
               {ref:1,msg:"提交失败",result:"" }
            end 
       end
    ################################################################
       get 'query_data' do 
           sections_id = params[:sections_id]    #为了组织机构，新增参数
           begin_time =params[:begin_time]           #开始时间
           end_time=params[:end_time]                #结束时间
           ref=RecordData.find_by_sql("SELECT b.name hotel_name,c.address,c.name address_name,a.*,concat('/uploads/record_data/pic/',a.id,'/',a.pic) pic_url  FROM
 record_data a,hotel_master b,hotel_info c
where a.hotel_address_id=c.id and c.master_id=b.id and date(a.upload_time)>='#{begin_time}' and date(a.upload_time)<='#{end_time}'  
and a.sections_id=#{sections_id} and b.sections_id=#{sections_id} and c.sections_id=#{sections_id}
order by upload_time desc  ")
           return {ref:0,msg:"提交成功",result:ref }
       end
    ################################################################
    end
end



def uploadfile(file,product_id)
        if !file["filename"].empty?
          @filename = file["filename"]
          original_filename= file["filename"]
          if file.size>6000000
            return "toobig"
          else
              #设置目录路径，如果目录不存在，生成新目录
              FileUtils.mkdir("#{Rails.root}/public/uploads/record_data/pic/#{product_id.to_s}") unless File.exist?("#{Rails.root}/public/uploads/record_data/pic/#{product_id.to_s}")
              #写入文件
              ##wb 表示通过二进制方式写，可以保证文件不损坏
              File.open("#{Rails.root}/public/uploads/record_data/pic/#{product_id.to_s}/#{@filename}", "wb") do |f|
                f.write(file["tempfile"].read)
              end
              return @filename
          end
        else
            return ""
        end
end


